print("haha-md-xd-wlk插件，版本：0.0.1.20250422")

local wowWatcher = CreateFrame("Frame", "BlueSquareFrame", UIParent);-- 创建一个新的框架
wowWatcher:SetFrameLevel(1);-- 设置框架的层级
wowWatcher:SetSize(100, 100) -- 宽度和高度都是100像素 -- 设置框架的尺寸
wowWatcher:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 0, 0) -- 距离屏幕左上角10,-10的位置 -- 设置框架的位置

-- 从左到右，颜色对应的含义
-- 1-1 单体开关，绿色为开,红色为关
-- 1-2 AOE开关，绿色为开，红色为关，预留位置，暂时没有
-- 1-3 裂伤（豹），绿色为需要打，红色为不需要打
-- 1-4 野蛮咆哮，绿色为需要打，红色为不需要打
-- 1-5 斜掠，绿色为需要打，红色为不需要打
-- 1-6 撕碎，绿色为需要打，红色为不需要打
-- 1-7 割裂，绿色为需要打，红色为不需要打
-- 1-8 精灵之火（野性），绿色为需要打，红色为不需要打
-- 1-9 凶猛撕咬，绿色为需要打，红色为不需要打

local colorSize = 5;

-- 1-1 单体开关，绿色为开,红色为关
local one_target_switch_wl_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
one_target_switch_wl_green:SetColorTexture(0, 1, 0, 1)
one_target_switch_wl_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 0, 0); -- 使纹理填充整个框架
one_target_switch_wl_green:SetSize(colorSize, colorSize);
one_target_switch_wl_green:SetAlpha(0);

local one_target_switch_wl_red = wowWatcher:CreateTexture(nil, "BACKGROUND")--创建纹理
one_target_switch_wl_red:SetColorTexture(1, 0, 0, 1)
one_target_switch_wl_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 0, 0);--设置框架相对锚点
one_target_switch_wl_red:SetSize(5, 5)
one_target_switch_wl_red:SetAlpha(1)

-- 间隔纹理
local splitTexture1 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture1:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture1:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 5, 0); -- 使纹理填充整个框架
splitTexture1:SetSize(1, colorSize);
splitTexture1:SetAlpha(1);

-- 1-2 AOE开关，绿色为开，红色为关，预留位置，暂时没有
local more_target_switch_wl_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
more_target_switch_wl_green:SetColorTexture(0, 1, 0, 1)
more_target_switch_wl_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 6, 0); -- 使纹理填充整个框架
more_target_switch_wl_green:SetSize(colorSize, colorSize);
more_target_switch_wl_green:SetAlpha(0);

local more_target_switch_wl_red = wowWatcher:CreateTexture(nil, "BACKGROUND")--创建纹理
more_target_switch_wl_red:SetColorTexture(1, 0, 0, 1)
more_target_switch_wl_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 6, 0);--设置框架相对锚点
more_target_switch_wl_red:SetSize(5, 5)
more_target_switch_wl_red:SetAlpha(1)

-- 间隔纹理
local splitTexture2 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture2:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture2:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 11, 0); -- 使纹理填充整个框架
splitTexture2:SetSize(1, colorSize);
splitTexture2:SetAlpha(1);

-- 1-3 裂伤（豹），绿色为需要打，红色为不需要打
local ls_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
ls_green:SetColorTexture(0, 1, 0, 1)
ls_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 12, 0);
ls_green:SetSize(colorSize, colorSize);
ls_green:SetAlpha(0);

local ls_red = wowWatcher:CreateTexture(nil, "BACKGROUND")
ls_red:SetColorTexture(1, 0, 0, 1)
ls_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 12, 0);
ls_red:SetSize(colorSize, colorSize)
ls_red:SetAlpha(1)

-- 间隔纹理
local splitTexture3 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture3:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture3:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 17, 0);
splitTexture3:SetSize(1, colorSize);
splitTexture3:SetAlpha(1);

-- 1-4 野蛮咆哮，绿色为需要打，红色为不需要打
local ympx_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
ympx_green:SetColorTexture(0, 1, 0, 1)
ympx_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 18, 0);
ympx_green:SetSize(colorSize, colorSize);
ympx_green:SetAlpha(0);

local ympx_red = wowWatcher:CreateTexture(nil, "BACKGROUND")
ympx_red:SetColorTexture(1, 0, 0, 1)
ympx_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 18, 0);
ympx_red:SetSize(colorSize, colorSize)
ympx_red:SetAlpha(1)

-- 间隔纹理
local splitTexture4 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture4:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture4:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 23, 0);
splitTexture4:SetSize(1, colorSize);
splitTexture4:SetAlpha(1);

-- 1-5 斜掠，绿色为需要打，红色为不需要打
local xl_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
xl_green:SetColorTexture(0, 1, 0, 1)
xl_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 24, 0);
xl_green:SetSize(colorSize, colorSize);
xl_green:SetAlpha(0);

local xl_red = wowWatcher:CreateTexture(nil, "BACKGROUND")
xl_red:SetColorTexture(1, 0, 0, 1)
xl_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 24, 0);
xl_red:SetSize(colorSize, colorSize)
xl_red:SetAlpha(1)

-- 间隔纹理
local splitTexture5 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture5:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture5:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 29, 0);
splitTexture5:SetSize(1, colorSize);
splitTexture5:SetAlpha(1);

-- 1-6 撕碎，绿色为需要打，红色为不需要打
local ss_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
ss_green:SetColorTexture(0, 1, 0, 1)
ss_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 30, 0);
ss_green:SetSize(colorSize, colorSize);
ss_green:SetAlpha(0);

local ss_red = wowWatcher:CreateTexture(nil, "BACKGROUND")
ss_red:SetColorTexture(1, 0, 0, 1)
ss_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 30, 0);
ss_red:SetSize(colorSize, colorSize)
ss_red:SetAlpha(1)

-- 间隔纹理
local splitTexture6 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture6:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture6:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 35, 0);
splitTexture6:SetSize(1, colorSize);
splitTexture6:SetAlpha(1);

-- 1-7 割裂，绿色为需要打，红色为不需要打
local gl_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
gl_green:SetColorTexture(0, 1, 0, 1)
gl_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 36, 0); -- 使纹理填充整个框架
gl_green:SetSize(5, 5);
gl_green:SetAlpha(0);

local gl_red = wowWatcher:CreateTexture(nil, "BACKGROUND")--创建纹理
gl_red:SetColorTexture(1, 0, 0, 1)
gl_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 36, 0);--设置框架相对锚点
gl_red:SetSize(5, 5)
gl_red:SetAlpha(1);

-- 间隔纹理
local splitTexture7 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture7:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture7:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 41, 0); -- 使纹理填充整个框架
splitTexture7:SetSize(1, colorSize);
splitTexture7:SetAlpha(1);

-- 1-8 精灵之火（野性），绿色为需要打，红色为不需要打
local jlzh_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
jlzh_green:SetColorTexture(0, 1, 0, 1)
jlzh_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 42, 0); -- 使纹理填充整个框架
jlzh_green:SetSize(5, 5);
jlzh_green:SetAlpha(0);

local jlzh_red = wowWatcher:CreateTexture(nil, "BACKGROUND")--创建纹理
jlzh_red:SetColorTexture(1, 0, 0, 1)
jlzh_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 42, 0);--设置框架相对锚点
jlzh_red:SetSize(5, 5)
jlzh_red:SetAlpha(1);

-- 间隔纹理
local splitTexture8 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture8:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture8:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 47, 0); -- 使纹理填充整个框架
splitTexture8:SetSize(1, colorSize);
splitTexture8:SetAlpha(1);

-- 1-9 凶猛撕咬，绿色为需要打，红色为不需要打
local xmsy_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
xmsy_green:SetColorTexture(0, 1, 0, 1)
xmsy_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 48, 0); -- 使纹理填充整个框架
xmsy_green:SetSize(5, 5);
xmsy_green:SetAlpha(0);

local xmsy_red = wowWatcher:CreateTexture(nil, "BACKGROUND")--创建纹理
xmsy_red:SetColorTexture(1, 0, 0, 1)
xmsy_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 48, 0);--设置框架相对锚点
xmsy_red:SetSize(5, 5)
xmsy_red:SetAlpha(1);

-- 间隔纹理
local splitTexture9 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture9:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture9:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 53, 0); -- 使纹理填充整个框架
splitTexture9:SetSize(1, colorSize);
splitTexture9:SetAlpha(1);

-- 工具函数区
-- 这段是一个独立的方法，可以被其他地方调用，用来获取目标身上的debuff信息，并封装到对象列表中
local function getDebuff(player, filter)
    if filter == nil then
        filter = "HARMFUL";
    end
    local debuffList = {}
    --读取对象身上的有害buff
    for i = 1, 40 do
        local name, icon, count, dispelType, duration, expirationTime, source, isStealable, nameplateShowPersonal, spellId, canApplyAura, isBossDebuff, castByPlayer = UnitAura(player, i, filter)
        if name then
            table.insert(debuffList, createDebuffInfo(name, count, expirationTime, source, castByPlayer, spellId, canApplyAura));
        else
            break
        end
    end
    return debuffList
end

-- 这段代码是用来创建debuff的对象，保存了名称和过期的时间
function createDebuffInfo(name, count, expirationTime, source, castByPlayer, spellId, canApplyAura)
    return {
        name = name,
        count = count,
        expirationTime = expirationTime,
        source = source,
        castByPlayer = castByPlayer,
        spellId = spellId,
        canApplyAura = canApplyAura
    }
end

-- 变量区
-- 用于表示当前是否有目标
local hasTarget = false;
-- 用于检测当前目标是否可以被攻击
local canAttack = false;
-- 检测当前目标是否已经死亡
local targetIsDead = false;

-- 事件区
wowWatcher:SetScript("OnUpdate", function()
    -- 检测当前是否有目标
    if UnitExists("target") then
        hasTarget = true;
    else
        hasTarget = false;
    end

    -- 检测当前是否可以被攻击
    if UnitCanAttack("player", "target") then
        canAttack = true;
    else
        canAttack = false;
    end

    -- 检测目标是否死亡
    if UnitIsDead("target") then
        targetIsDead = true;
    else
        targetIsDead = false;
    end

    -- 获取敌人身上的debuff
    local targetDebuffList = getDebuff("target", "PLAYER|HARMFUL");
    -- 获取我自己身上的debuff
    local playerBuffList = getDebuff("player", "HELPFUL");
    -- 获取连击点数
    local comboPoints = GetComboPoints("player", "target");
    -- 当前目标是否有割裂
    local hasRip = false;
    local ripDuration = 0;

    -- 割裂的剩余时间
    for index, debuffInfo in ipairs(targetDebuffList) do
        if debuffInfo.name == "割裂" then
            hasRip = true;
            ripDuration = debuffInfo.expirationTime - GetTime()
            break;
        end
    end

    checkLs(targetDebuffList);
    checkYmpx(playerBuffList, targetDebuffList);
    -- 檢查斜掠有沒有
    checkXl(targetDebuffList);
    -- 检查割裂是否需要打
    checkGl(targetDebuffList, comboPoints, hasRip);

    checkJlzh();
    checkXmsy(comboPoints, ripDuration, hasRip);
end);

-- 检测裂伤（豹）
function checkLs(targetDebuffList)
    if not IsUsableSpell('裂伤（豹）') then
        return
    end

    local lsDebuff = false;

    -- 没有目标直接返回
    if not hasTarget then
        ls_red:SetAlpha(1);
        ls_green:SetAlpha(0);
        return ;
    end
    -- 不能攻擊返回
    if not canAttack then
        ls_red:SetAlpha(1);
        ls_green:SetAlpha(0);
        return ;
    end
    -- 目标已经死亡情况下，不打旋风斩
    if targetIsDead then
        ls_red:SetAlpha(1);
        ls_green:SetAlpha(0);
        return ;
    end

    for index, debuffInfo in ipairs(targetDebuffList) do
        if debuffInfo.name == "裂伤（豹）" and (debuffInfo.expirationTime - GetTime()) > 1 then
            lsDebuff = true;
            break ;
        end
    end

    -- 没有裂伤（豹）的时候，就标识为绿色
    if lsDebuff then
        ls_red:SetAlpha(1);
        ls_green:SetAlpha(0);
    else
        ls_red:SetAlpha(0);
        ls_green:SetAlpha(1);
    end

end

function checkYmpx(playerBuffList, targetDebuffList)
    -- 先检测是否有这个技能
    local usable, nomana = IsUsableSpell("野蛮咆哮");
    if not usable then
        ympx_red:SetAlpha(1);
        ympx_green:SetAlpha(0);
        return ;
    end

    local ympxInfo = nil;
    local glInfo = nil;
    -- 获取野蛮咆哮的信息
    for index, debuffInfo in ipairs(playerBuffList) do
        if debuffInfo.name == "野蛮咆哮" and debuffInfo.source == "player" then
            ympxInfo = debuffInfo;
            break;
        end
    end
    -- 获取割裂的信息
    for index, debuffInfo in ipairs(targetDebuffList) do
        if debuffInfo.name == "割裂" and debuffInfo.source == "player" then
            glInfo = debuffInfo;
            break;
        end
    end

    if glInfo then
        if ((glInfo.expirationTime - GetTime()) < 7) then
            ympx_red:SetAlpha(1);
            ympx_green:SetAlpha(0);
        else
            if ympxInfo then
                if ((ympxInfo.expirationTime - GetTime()) < 8) then
                    ympx_red:SetAlpha(0);
                    ympx_green:SetAlpha(1);
                else
                    ympx_red:SetAlpha(1);
                    ympx_green:SetAlpha(0);
                end
            else
                ympx_red:SetAlpha(1);
                ympx_green:SetAlpha(0);
            end
        end
    else
        if ympxInfo then
            ympx_red:SetAlpha(1);
            ympx_green:SetAlpha(0);
        else
            ympx_red:SetAlpha(0);
            ympx_green:SetAlpha(1);
        end
    end

end

-- 檢查斜掠
function checkXl(targetDebuffList)
    local usable, nomana = IsUsableSpell("斜掠");
    if not usable then
        xl_red:SetAlpha(1);
        xl_green:SetAlpha(0);
        return ;
    end

    -- 没有目标直接返回
    if not hasTarget then
        xl_red:SetAlpha(1);
        xl_green:SetAlpha(0);
        return ;
    end
    -- 不能攻擊返回
    if not canAttack then
        xl_red:SetAlpha(1);
        xl_green:SetAlpha(0);
        return ;
    end
    -- 目标已经死亡情况下，不打旋风斩
    if targetIsDead then
        xl_red:SetAlpha(1);
        xl_green:SetAlpha(0);
        return ;
    end

    local xlDebuff = false;
    for index, debuffInfo in ipairs(targetDebuffList) do
        if debuffInfo.name == "斜掠" then
            xlDebuff = true;
            break ;
        end
    end

    -- 没有裂伤（豹）的时候，就标识为绿色
    if xlDebuff then
        xl_red:SetAlpha(1);
        xl_green:SetAlpha(0);
    else
        xl_red:SetAlpha(0);
        xl_green:SetAlpha(1);
    end
end

function checkGl(targetDebuffList, comboPoints, hasRip)
    local usable, nomana = IsUsableSpell("割裂");
    if not usable then
        gl_red:SetAlpha(1);
        gl_green:SetAlpha(0);
        return ;
    end

    -- 没有目标直接返回
    if not hasTarget then
        gl_red:SetAlpha(1);
        gl_green:SetAlpha(0);
        return ;
    end
    -- 不能攻擊返回
    if not canAttack then
        gl_red:SetAlpha(1);
        gl_green:SetAlpha(0);
        return ;
    end
    -- 目标已经死亡情况下，不打旋风斩
    if targetIsDead then
        gl_red:SetAlpha(1);
        gl_green:SetAlpha(0);
        return ;
    end

    -- 检查现在是不是5星，如果小于5星直接返回，不打割裂
    if comboPoints < 5 then
        gl_red:SetAlpha(1);
        gl_green:SetAlpha(0);
        return ;
    end

    -- 如果有割裂就返回
    if hasRip then
        gl_red:SetAlpha(1);
        gl_green:SetAlpha(0);
    end

    -- 上面的检测都符合，则打割裂
    gl_red:SetAlpha(0);
    gl_green:SetAlpha(1);
end

function checkJlzh()
    local name, iconTexture, tier, column, rank, maxRank, isExceptional, available = GetTalentInfo(3, 7);
    if rank == 0 then
        return;
    end

    local usable, nomana = IsUsableSpell("精灵之火（野性）");
    if not usable then
        jlzh_red:SetAlpha(1);
        jlzh_green:SetAlpha(0);
        return ;
    end

    -- 没有目标直接返回
    if not hasTarget then
        jlzh_red:SetAlpha(1);
        jlzh_green:SetAlpha(0);
        return ;
    end
    -- 不能攻擊返回
    if not canAttack then
        jlzh_red:SetAlpha(1);
        jlzh_green:SetAlpha(0);
        return ;
    end
    -- 目标已经死亡情况下，不打旋风斩
    if targetIsDead then
        jlzh_red:SetAlpha(1);
        jlzh_green:SetAlpha(0);
        return ;
    end

    local start, duration, enabled, modRate = GetSpellCooldown(16857);
    if start == 0 or duration == 1.5 then
        jlzh_red:SetAlpha(0);
        jlzh_green:SetAlpha(1);
    else
        jlzh_red:SetAlpha(1);
        jlzh_green:SetAlpha(0);
    end
end

-- 检测凶猛撕咬
function checkXmsy(comboPoints, ripDuration, hasRip)
    local usable, nomana = IsUsableSpell("凶猛撕咬");
    if not usable then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end

    -- 没有目标直接返回
    if not hasTarget then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end
    -- 不能攻擊返回
    if not canAttack then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end
    -- 目标已经死亡情况下，不打旋风斩
    if targetIsDead then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end

    -- 不到5星不放凶猛撕咬
    if comboPoints < 5 then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end

    -- 没有割裂直接就不能打凶猛撕咬
    if not hasRip then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end

    -- 判断割裂的剩余时间要小于8秒，就不打凶猛撕咬了
    if ripDuration < 8 then
        xmsy_red:SetAlpha(1);
        xmsy_green:SetAlpha(0);
        return ;
    end

    xmsy_red:SetAlpha(1);
    xmsy_green:SetAlpha(0);
end

-- 单目标输出打开
function startOneTargetSwitch()
    one_target_switch_wl_red:SetAlpha(0);
    one_target_switch_wl_green:SetAlpha(1);

    more_target_switch_wl_red:SetAlpha(1);
    more_target_switch_wl_green:SetAlpha(0);
end

-- 多目标输出打开
function startMoreTargetSwitch()
    more_target_switch_wl_red:SetAlpha(0);
    more_target_switch_wl_green:SetAlpha(1);

    one_target_switch_wl_red:SetAlpha(1);
    one_target_switch_wl_green:SetAlpha(0);
end

-- 关闭所有输出
function closeSwitch()
    -- 关闭单体输出
    one_target_switch_wl_red:SetAlpha(1);
    one_target_switch_wl_green:SetAlpha(0);

    more_target_switch_wl_red:SetAlpha(1);
    more_target_switch_wl_green:SetAlpha(0);
end

local myMacro = [=[
/run print("Hello")
/run print("World!")
]=]

local frame = CreateFrame("Button", "123123", UIParent, "SecureActionButtonTemplate");
frame:SetAttribute("type", "macro")
frame:SetAttribute("macrotext", myMacro);

local bts = CreateFrame("Button", "STarget30", UIParent, "SecureActionButtonTemplate")
bts:SetAttribute("type", "macro")
bts:SetAttribute("macrotext", "/target player")

-- ALT-CTRL-U 应该是被其他按键占用了，用不了
-- 可以正常打
SetOverrideBindingSpell(frame, true, "CTRL-I", "野蛮咆哮");
-- 可以正常打
SetOverrideBindingSpell(frame, true, "CTRL-O", "斜掠");
-- 可以正常打
SetOverrideBindingSpell(frame, true, "CTRL-K", "割裂");
-- 可以正常打
SetOverrideBindingSpell(frame, true, "CTRL-J", "精灵之火（野性）");
-- 可以正常打
SetOverrideBindingSpell(frame, true, "CTRL-L", "撕碎");